//
//  UITextfield.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/1.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
extension UITextField {
    func cheakNumberFormat(digit:Int,range: NSRange,string: String,interger:Int) -> Bool {
        let newString = (self.text! as NSString).replacingCharacters(in: range, with: string)
        if string != "" {
            let dotLocation = (self.text! as NSString).range(of: ".").location
            if dotLocation == NSNotFound && range.location != 0 {
                if !(string == "." && range.location == interger) {
                if range.location >= interger {
                    print("整数位数不能超出最长位数")
                    return false
                 }
               }
            }
        }
        if newString.ch_length == 1 && newString == "." {
            return false
        }
       
        let expression = String(format: "^[0-9]*((\\.|,)[0-9]{0,%d})?$", digit)
        
        let regex = try! NSRegularExpression(pattern: expression, options: NSRegularExpression.Options.allowCommentsAndWhitespace)
        let numberOfMatches = regex.numberOfMatches(in: newString, options:NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, (newString as NSString).length))
        return numberOfMatches != 0
    }
    
    func judgePassRule(pass:String) -> Bool{
        let regex = "^([0-9a-zA-Z]+$)"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with:pass)
    }
    
    func setPlaceholderColor() {
        if ThemeManager.default.currentThemeMode() == .night {
            self.setValue(UIColor(hexStr: "#616979"), forKeyPath: "_placeholderLabel.textColor")
        }else {
            self.setValue(UIColor(hexStr: "#b1b2b2"), forKeyPath: "_placeholderLabel.textColor")
        }
    }
    

    
    
}
